<?php

require_once "../common/citizen.php";

$citizens = array();

$data_file = 'data.dat';

$lines = file($data_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($lines as $key => $line) {

	$a = explode(',', $line);
	
	if ($a[8] == 0) {
		$citizen = new Citizen($a[0],$a[1],$a[2],$a[3],$a[4],$a[5],$a[6],$a[7],FALSE);
		$citizen->Process();
		$citizens[] = $citizen;
	}
	
}

// sort by influ
usort($citizens, array("Citizen", "cmp_influ"));

$level_a = 100;
$level_b = 300;
$level_s = 550;
$level_ss = 1000;

$c = array();
$rank_a = 0;
$rank_b = 0;
$rank_s = 0;
$rank_ss = 0;

$body_news = '';
$body_raw = '';

foreach($citizens as $key => $one) {

	$body_news .= sprintf("<%d> [b]%s[/b], %d, %s, %d\r\n",$key+1,$one->name,$one->strength,$one->rank_name,$one->influ);
	$body_raw .= sprintf("<%d> %s, %d, %s, %d\r\n",$key+1,$one->name,$one->strength,$one->rank_name,$one->influ);
	// catagory
	if ($one->influ >= $level_ss) {
		$rank_ss++;
	} else if ($one->influ >= $level_s) {
		$rank_s++;
	} else if ($one->influ >= $level_b) {
		$rank_b++;
	} else if ($one->influ >= $level_a) {
		$rank_a++;
	}

}

$content_news = '';
$content_raw = "<html><body><pre>\r\n";

$content_news .= sprintf("SS(>$level_ss): %d\r\n", $rank_ss);
$content_news .= sprintf("S(>$level_s): %d\r\n", $rank_s);
$content_news .= sprintf("B(>$level_b): %d\r\n", $rank_b);
$content_news .= sprintf("A(>$level_a): %d\r\n", $rank_a);
$content_news .= sprintf("\r\n");

$content_raw .= $content_news;

$content_news .= $body_news;
$content_raw .= $body_raw;

$content_raw .= "</pre></body></html>";

$file_news = fopen('../out/dph.txt', 'w');
fwrite($file_news, $content_news);
fclose($file_news);

$file_raw = fopen('../out/dph.html', 'w');
fwrite($file_raw, $content_raw);
fclose($file_raw);

// sort by work
usort($citizens, array("Citizen", "cmp_work"));

$body_news = '';
$body_raw = '';

unset($c);
$i = 0;
$c = array();

foreach($citizens as $key => $one) {
	++$i;
	$points = $one->work;
	$level = 6;
	$base = 5000;
	
	if ($points < 5000) {
		break;
	}

	while ($points >= $base*2) {
		$base *= 2;
		$level++;
	}
	
	$percent = ($points - $base) * 100 / $base;
	$body_news .= sprintf("<%d> [b]%s[/b], %d (%.2f%%)\r\n", $i, $one->name, $level, $percent);
	$body_raw .= sprintf("<%d> %s, %d (%.2f%%)\r\n", $i, $one->name, $level, $percent);

	if (!array_key_exists($level, $c)) {
		$c[$level] = 0;
	}
	$c[$level] += 1;
}

$content_news = '';
$content_raw = "<html><body><pre>\r\n";

foreach($c as $level => $num) {
	$content_news .= sprintf("Level %d: %d\r\n", $level, $num);
}
$content_news .= sprintf("\r\n");

$content_raw .= $content_news;

$content_news .= $body_news;
$content_raw .= $body_raw;

$content_raw .= "</pre></body></html>";

$file_news = fopen('../out/work.txt', 'w');
fwrite($file_news, $content_news);
fclose($file_news);

$file_raw = fopen('../out/work.html', 'w');
fwrite($file_raw, $content_raw);
fclose($file_raw);
	
?>